﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using System.Text;
using System.Collections.Generic;

public partial class Task_EvaluateList : System.Web.UI.Page
{
    //设置每页行数
    private int pageSize = 20;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StartDate.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToShortDateString();
            EndDate.Text = DateTime.Now.ToShortDateString();
            BindGrid(0);
        }
    }
    private void BindGrid(int pageIndex)
    {
        ViewState["PageIndex"] = pageIndex;
        StringBuilder selectCommandText = new StringBuilder();
        selectCommandText.Append("select * from tasks where ParentId=0 and CreateDepartment='" + Yipusoft.Account.BLL.User.GetUser(Context.User.Identity.Name).DepartmentID + "'");
        selectCommandText.Append(" and ((StartDate>='" + StartDate.Text + "' and StartDate<='" + EndDate.Text + "')");
        selectCommandText.Append(" or (StartDate<'" + EndDate.Text + "' and EndDate>='" + EndDate.Text + "'))");

        UserGrid.DataSource = SplitPage.GetPageData(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        UserGrid.DataBind();

        int totalCount = SplitPage.GetRowCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        ViewState["PageCount"] = SplitPage.GetPageCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * pageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(JumpList.SelectedValue));
    }
    public string GetTaskTypeName(int taskType)
    {
        return TaskType.GetTaskType(taskType).TypeName.Substring(0, 1);
    }
    public string GetTitle(string title)
    {
        return title.Length > 28 ? title.Substring(0, 28) + "..." : title;
    }
    public string GetLockText(bool locked)
    {
        return locked ? "解锁" : "锁定";
    }
    public string GetUserCount(int taskId)
    {
        List<TaskUserInfo> users = Task.GetTask(taskId).Users;
        int x = users.Count;
        int y = 0;
        int z = 0;
        foreach (TaskUserInfo user in users)
        {
            if (user.Evaluated)
            {
                y++;
            }
            else
            {
                z++;
            }
        }
        return z.ToString();
    }
    protected void UserGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int taskId = (int)UserGrid.DataKeys[e.NewSelectedIndex].Value;
        if (Task.GetTask(taskId).Locked)
        {
            Task.Lock(taskId, false);
        }
        else
        {
            Task.Lock(taskId, true);
        }
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        BindGrid(0);
    }
}